package com.wythe.mall.adapter;

import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.wythe.mall.R;
import com.wythe.mall.model.AddressBean;

import java.util.ArrayList;

public class AddressListAdapter extends RecyclerView.Adapter<AddressListAdapter.ViewHolder> {

    private final ArrayList<AddressBean> arrayList;
    private OnItemClickListener onItemClickListener;
    private OnViewClickListener onViewClickListener;

    public AddressListAdapter(ArrayList<AddressBean> arrayList) {
        this.arrayList = arrayList;
        this.onItemClickListener = null;
    }

    @Override
    public int getItemCount() {

        return arrayList.size();

    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {

        final int positionInt = position;
        final AddressBean bean = arrayList.get(position);

        holder.nameTextView.setText(bean.getName());
        holder.mobileTextView.setText(bean.getMobile());
        holder.areaTextView.setText(bean.getProvince()+" "+bean.getCity()+" "+bean.getDistrice()+" "+bean.getAddress());
        holder.defaultTextView.setVisibility(bean.getStatus() == 1 ? View.VISIBLE : View.INVISIBLE);
        holder.mainCheckBox.setVisibility(bean.getStatus() == 1 ? View.VISIBLE : View.INVISIBLE);

        holder.editTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener != null) {
                    onItemClickListener.onEdit(positionInt, bean);
                }
            }
        });

        holder.deleteTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener != null) {
                    onItemClickListener.onDelete(positionInt, bean);
                }
            }
        });


        holder.mainRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onViewClickListener != null) {
                    onViewClickListener.onClick(positionInt, bean);
                }
            }
        });
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup group, int viewType) {
        View view = LayoutInflater.from(group.getContext()).inflate(R.layout.item_list_address, group, false);
        return new ViewHolder(view);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {

        this.onItemClickListener = listener;

    }

    public void setOnViewClickListener(OnViewClickListener listener) {

        this.onViewClickListener = listener;

    }

    public interface OnItemClickListener {

        void onEdit(int position, AddressBean bean);

        void onDelete(int position, AddressBean bean);

    }

    public interface OnViewClickListener {
        void onClick(int position, AddressBean bean);
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private RelativeLayout mainRelativeLayout;
        private AppCompatTextView nameTextView;
        private AppCompatTextView mobileTextView;
        private AppCompatTextView areaTextView;
        private AppCompatTextView defaultTextView;
        private AppCompatTextView deleteTextView;
        private AppCompatTextView editTextView;
        private AppCompatCheckBox mainCheckBox;

        private ViewHolder(View view) {
            super(view);
            mainRelativeLayout = (RelativeLayout) view.findViewById(R.id.mainRelativeLayout);
            nameTextView = (AppCompatTextView) view.findViewById(R.id.nameTextView);
            mobileTextView = (AppCompatTextView) view.findViewById(R.id.mobileTextView);
            areaTextView = (AppCompatTextView) view.findViewById(R.id.areaTextView);

            defaultTextView = (AppCompatTextView) view.findViewById(R.id.defaultTextView);
            deleteTextView = (AppCompatTextView) view.findViewById(R.id.deleteTextView);
            editTextView = (AppCompatTextView) view.findViewById(R.id.editTextView);
            mainCheckBox = (AppCompatCheckBox) view.findViewById(R.id.mainCheckBox);
        }

    }

}
